<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SanPhan_TrangChu extends CI_Model {
 
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
	 
	  // lấy dữ liệu theo từng phần 
        function DanhSach($sodong, $tudong){ 
			
            $query =  $this->db->get('sanpham',$sodong,$tudong); 
            return $query->result_array(); 
        } 
        function DanhSach_MaL($MaL,$sodong, $tudong){ 
			$this->db->where("Ma_DM",$MaL);
            $query =  $this->db->get('sanpham',$sodong,$tudong); 
            return $query->result_array(); 
        }
        // đếm tổng số record trong table book 
        function DemDanhSach(){ 
            return $this->db->count_all('sanpham'); 
        }  
		 
		function LaySP($MaSP){
				$this->db->select("*"); 
				$this->db->where("MaSP",$MaSP);
				$data = $this->db->get("sanpham");
				return $data->row_array();
			 
		}
		
		function LayTop2( ){ 
			$query=$this->db->query("SELECT * FROM SanPham   ORDER BY MaSP DESC LIMIT 2") ; 
       		return $query->result_array();
        }
		function SPXemNhieu( ){ 
			$query=$this->db->query("SELECT * FROM SanPham   ORDER BY SoLuotXem DESC LIMIT 6") ; 
       		return $query->result_array();
        }
		function SPMuaNhieu( ){ 
			$query=$this->db->query("SELECT * FROM SanPham   ORDER BY SoLuotDatHang DESC LIMIT 6") ; 
       		return $query->result_array();
        }
		
		function SPMoi( ){ 
			$query=$this->db->query("SELECT * FROM SanPham   ORDER BY MaSP DESC LIMIT 6") ; 
       		return $query->result_array();
        } 
		
		function DemDanhSach_MaL($MaL){  
			$this->db->select('masp');
			$this->db->from('sanpham');
			$this->db->where('Ma_DM',$MaL);
			$query = $this->db->get();
			return $query->num_rows();
        }
		
		function DemDanhSach_TK($query){  
			$query1;
			if(!empty($query['ma_dm'])&&!empty($query['key']))
			{
				if($query['ma_dm']==0)
				{
					$query1=$this->db->query("SELECT masp FROM SanPham where  tensanpham like '%".$query['key']."%' ORDER BY MaSP ") ; 
					}
				else
				$query1=$this->db->query("SELECT masp FROM SanPham where ma_dm=".$query['ma_dm']." and tensanpham like '%".$query['key']."%' ORDER BY MaSP ") ; }
			else if(!empty($query['ma_dm']))	
			{$query1=$this->db->query("SELECT masp FROM SanPham where ma_dm=".$query['ma_dm']." ORDER BY MaSP ") ; }
			else if(!empty($query['key']))
			{$query1=$this->db->query("SELECT masp FROM SanPham where  tensanpham like '%".$query['key']."%' ORDER BY MaSP ") ; 			}
			else	
			{$query1=$this->db->query("SELECT masp FROM SanPham ORDER BY MaSP") ; }
			return $query1->num_rows();
      	  }	

		function DanhSach_TK( $query,$sodong, $tudong ){ 
			$query1;
			if(!empty($query['ma_dm'])&&!empty($query['key']))
			{$query1=$this->db->query("SELECT * FROM SanPham where ma_dm=".$query['ma_dm']." and tensanpham like '%".$query['key']."%' ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ; }
			else if(!empty($query['ma_dm']))	
			{$query1=$this->db->query("SELECT * FROM SanPham where ma_dm=".$query['ma_dm']." ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ; }
			else if(!empty($query['ma_dm']))
			{$query1=$this->db->query("SELECT * FROM SanPham where  tensanpham like '%".$query['key']."%' ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ; }
			else	
			{$query1=$this->db->query("SELECT * FROM SanPham ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ; }
			return $query1->result_array();
        }
		//SELECT * FROM topic WHERE subject LIKE '%array[1]%'
		function TimTheoChu( $KyTu,$sodong, $tudong ){ 
			$query=$this->db->query("SELECT * FROM SanPham WHERE TenSanPham LIKE '".$KyTu."%'  ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ;
			return $query->result_array();
        }
		  
 		function Dem_TimTheoChu($KyTu){   
			$query=$this->db->query("SELECT * FROM SanPham WHERE TenSanPham LIKE '".$KyTu."%'") ;
			return $query->num_rows();
        }
		/////////////Nhạn Xet
		function ThemNhanXet($data)
    {
        $this->db->insert('danhgia', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	  function LayNhanXet_MaSP($MaSP,$sodong, $tudong){  
			$query=$this->db->query("SELECT * FROM danhgia LEFT JOIN taikhoan ON danhgia.Ma_TK=taikhoan.MaTK WHERE danhgia.Ma_SP=".$MaSP."  ORDER BY MaDG DESC LIMIT ".$tudong.",".$sodong) ;
			return $query->result_array(); 
        }
		function DemNhanXet_MaSP($MaSP){  
			$this->db->select('*');
			$this->db->from('danhgia');
			$this->db->where('Ma_SP',$MaSP);
			$query = $this->db->get();
			return $query->num_rows();
        }
		
		//////////////Tim Tin
		function TimTheoTen( $Ten,$sodong, $tudong ){ 
			$query=$this->db->query("SELECT * FROM SanPham WHERE TenSanPham LIKE '%".$Ten."%'  ORDER BY MaSP DESC LIMIT ".$sodong.",".$tudong) ;
			return $query->result_array();
        }
		  
 		function Dem_TimTheoTen($Ten){   
			$query=$this->db->query("SELECT * FROM SanPham WHERE TenSanPham LIKE '%".$Ten."%'") ;
			return $query->num_rows();
        }
}

?>